package manager

import (
	"NovelBook/logic/worker"
	"NovelBook/utils/auth"
	"NovelBook/utils/response"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

// GetAllWorkers 获取所有图书馆工作人员信息
//
//	@Summary		获取所有图书馆工作人员信息
//	@Description	获取所有图书馆工作人员信息
//	@Tags			Manager
//	@Accept			json
//	@Produce		json
//	@Security		ApiKeyAuth
//	@Param			Authorization	header		string								true	"`Bearer Token` 使用空格分隔`Bearer`与`Token`"
//	@Success		1000			{object}	msg.ResMsg{data=[]models.Worker}	"获取成功"
//	@Failure		400				{object}	msg.Code1001						"请求参数错误"
//	@Failure		401				{object}	msg.Code1005						"无效的`Token`"
//	@Failure		500				{object}	msg.Code1003						"系统繁忙，请稍后再试"
//	@Router			/manager/worker/info-all [get]
func GetAllWorkers(c *gin.Context) {
	idAny, exist := c.Get(auth.ManagerIDKey)
	id := idAny.(uint)
	if !exist || id < 1 {
		response.Fail(c, http.StatusUnauthorized, response.CodeTokenInvalid)
		return
	}

	workers, err := worker.GetWorkers()
	if err != nil {
		response.FailWithMsg(c, http.StatusInternalServerError, response.CodeSystemBusy, "system busy")
		return
	}
	response.Success(c, workers)
}

// 在职员工

func GetWorkersOn(c *gin.Context) {
	idAny, exist := c.Get(auth.ManagerIDKey)
	id := idAny.(uint)
	if !exist || id < 1 {
		response.Fail(c, http.StatusUnauthorized, response.CodeTokenInvalid)
		return
	}

	workers, err := worker.GetWorkersOn()
	if err != nil {
		response.FailWithMsg(c, http.StatusInternalServerError, response.CodeSystemBusy, "system busy")
		return
	}
	response.Success(c, workers)
}

// 离职员工

func GetWorkersOff(c *gin.Context) {
	idAny, exist := c.Get(auth.ManagerIDKey)
	id := idAny.(uint)
	if !exist || id < 1 {
		response.Fail(c, http.StatusUnauthorized, response.CodeTokenInvalid)
		return
	}

	workers, err := worker.GetWorkersOff()
	if err != nil {
		response.FailWithMsg(c, http.StatusInternalServerError, response.CodeSystemBusy, "system busy")
		return
	}
	response.Success(c, workers)
}

// GetWorkerByID 通过ID获取员工信息
//
//	@Summary		通过ID获取员工信息
//	@Description	通过WorkerID查询一个或多个员工
//	@Tags			Manager
//	@Accept			json
//	@Produce		json
//	@Security		ApiKeyAuth
//	@Param			Authorization	header		string								true	"`Bearer Token` 使用空格分隔`Bearer`与`Token`"
//	@Param			IDs				params		msg.ID								true	"WorkerIDs 要查询的WorkerID"
//	@Success		1000			{object}	msg.ResMsg{data=[]models.Worker}	"获取成功"
//	@Failure		400				{object}	msg.Code1001						"请求参数错误"
//	@Failure		401				{object}	msg.Code1005						"无效的`Token`"
//	@Failure		500				{object}	msg.Code1003						"系统繁忙，请稍后再试"
//	@Router			/manager/worker/info-ID [get]
func GetWorkerByID(c *gin.Context) {
	idAny, exist := c.Get(auth.ManagerIDKey)
	id := idAny.(uint)
	if !exist || id < 1 {
		response.Fail(c, http.StatusUnauthorized, response.CodeTokenInvalid)
		return
	}

	IDs := c.QueryArray("ID")
	if len(IDs) == 0 {
		response.Fail(c, http.StatusBadRequest, response.CodeParamsInvalid)
		return
	}
	for _, ID := range IDs {
		if _, err := strconv.Atoi(ID); err != nil {
			response.Fail(c, http.StatusBadRequest, response.CodeParamsInvalid)
			return
		}
	}

	workers, err := worker.GetWorkersByID(IDs)
	if err != nil {
		response.FailWithMsg(c, http.StatusInternalServerError, response.CodeSystemBusy, "system busy")
		return
	}
	response.Success(c, workers)
}
